import ajaxAsync from '../../../utils/ajaxAsync';
import { TimeTableModel } from '../models/TimeTableModel';

// 模拟数据
function mockData(direction) {
  return new Promise((resolve) => {
    let timeArr = [
      '06:30',
      '07:30',
      '09:30',
      '10:30',
      '12:30',
      '13:30',
      '15:30',
      '16:30',
      '18:30',
      '19:30'
    ]
    let table = [
      new TimeTableModel(
        '1',
        '夏季发车时刻表',
        '2',
        '2018-08-02',
        '8月31日',
        timeArr
      ),
      new TimeTableModel(
        '2',
        '秋季发车时刻表',
        '3',
        '2018-08-03',
        '12月31日',
        timeArr
      )
    ]
    setTimeout(function() {
      let tables = direction === 2 ? [] : table
      resolve({ tables, price: tables.length + '.00' })
    }, 500)
  })
}
/**
 * 判断是否在时间段内
 *
 * @param {string} str1 较小的时间字符串
 * @param {string} str2 较大的时间字符串
 * @returns
 */
function isInDate(str1, str2) {
  let date1 = new Date('2012/' + str1.replace(/-/g, '/')) // ios处理
  let date2 = new Date('2012/' + str2.replace(/-/g, '/')) // ios处理
  let date2Next
  if (date1 > date2) {
    date2Next = new Date(
      `${date2.getFullYear() + 4}/${date2.getMonth() + 1}/${date2.getDate()}`
    )
  }
  let nowDate = new Date()
  let dateNow = new Date(`2012/${nowDate.getMonth() + 1}/${nowDate.getDate()}`)
  let dateNowNext = new Date(
    `${dateNow.getFullYear() + 4}/${dateNow.getMonth() +
      1}/${dateNow.getDate()}`
  )
  if (date2Next) {
    return dateNow >= date1 || dateNowNext <= date2Next
  } else {
    return dateNow >= date1 && dateNow <= date2
  }
}
/**
 * 转换为xxxx-xx-xx格式日期字符串
 *
 * @param {string} originStr 原字符串格式 08-02
 */
function getTimeStr(originStr) {
  let newStr = originStr.replace(/-/g, '/')
  let thisYear = new Date().getFullYear()
  if (newStr === '02/29' && thisYear % 4 !== 0) {
    newStr = '02/28'
  }
  let nowDate = new Date()
  // if (
  //   new Date(`${thisYear}/${newStr}`) <
  //   new Date(`${thisYear}/${nowDate.getMonth() + 1}/${nowDate.getDate()}`)
  // ) {
  //   thisYear += 1
  // }
  // 返回日期和现在日期超过300天年份减1
  if (new Date(`${thisYear}/${newStr}`).getTime() - new Date(`${thisYear}/${nowDate.getMonth() + 1}/${nowDate.getDate()}`).getTime() > 1000 * 60 * 60 * 24 * 300) {
    thisYear -= 1
  }
  if (originStr === '02-29' && thisYear % 4 !== 0) {
    originStr = '02-28'
  }
  return `${thisYear}-${originStr}`
}
/**
 * 生成*月*日的日期字符串
 *
 * @param {string} dateStr 源日期字符串，如06-01
 * @returns
 */
function genDateStr(dateStr) {
  let newStr = '2012/' + dateStr.replace(/-/g, '/') // ios处理
  let date = new Date(newStr)
  return `${date.getMonth() + 1}月${date.getDate()}日`
}
/**
 * 获取时刻表
 *
 * @param {number} routeId 支线id
 * @param {number} direction 行驶方向（1、上行；2、下行）
 * @returns
 */
function getSchedules(routeId, direction) {
  // if (parseInt('1') === 1) return mockData(direction)

  return ajaxAsync('/bus/route/schedules', {
    SUB_ROUTEID: routeId,
    BUS_DIRECTION: direction
  }).then(res => {
    let { COUNT, LIST } = res
    if (COUNT && COUNT > 0 && LIST) {
      let tables = LIST.map((i, index) => {
        let { SCHEDULE_NAME, START_DATE, END_DATE, PRICE, SCHEDULE_INFOS } = i
        // 格式化时间字符串
        SCHEDULE_INFOS = (SCHEDULE_INFOS || []).map(i => {
          return i.length === 4 ? `0${i}` : i
        })
        return new TimeTableModel(
          `timetable${index}`,
          SCHEDULE_NAME || '',
          PRICE,
          getTimeStr(START_DATE),
          getTimeStr(END_DATE),
          SCHEDULE_INFOS
        )
      })
      return {
        tables
      }
    } else {
      return { tables: [] }
    }
  })
}
/**
 * 查询票价
 *
 * @param {Number} subrouteID 线路id
 * @param {Number} direction 方向id
 */
function getPrice(routeId, direction) {
  return ajaxAsync('/bus/route/today_price', {
    SUB_ROUTEID: parseInt(routeId),
    BUS_DIRECTION: parseInt(direction)
  }).then(res => {
    let { PRICE } = res
    return { price: PRICE }
  })
}
export {
  mockData,
  ajaxAsync,
  getTimeStr,
  getPrice,
  getSchedules,
  genDateStr,
  isInDate
}
